<?php
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/xmls.php
#	FUNCTION:		Handling the xmls and xml classes, as well as page-related database functions
#	AUTHOR:			Adrian Liu
#	Modified:		03/08/2011
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/**
 * Pages() contains Page objects for the pages in the site
*/
class Xmls {
	
	# Array of pages
	var $xml_array = array();
	
	/**
	 * Xmls() is the constructor.
	 *
	 * @param base_page_array is the array of pages to add on instantiation
	*/
	function Xmls($base_page_array = array()) {
		
		# For each page
		foreach ($base_page_array as $key => $tag) {

			# Add the page
			$this -> addTag($tag);
		}
	}
	
	/**
	 * addTag() adds a tag
	 *
	 * @param tag is the tag object to add
	*/
	function addTag($tag) {

		# If page passed
		if ($tag) {

			# If doesn't already exist
			if ($this -> getTag($tag -> getID()) === false) {

				# Add
				$this -> xml_array[$tag -> getID()] = $tag;

			# Add error
			} else {

				addError("Xmls -> addTag(): Xml '" . $tag -> getID() . "' already exists, can't be added again.");
			}
		}
	}
	
	/**
	 * getTag() return a page object
	*/
	function getTag($tag_id = "") {

		if (is_string($tag_id) || is_integer($tag_id)) {

			# If set
			if (array_key_exists($tag_id, $this -> xml_array)) {

				# Return page object
				return $this -> xml_array[$tag_id];

			} else {

				# Return failure
				return false;
			}
		} else {

			return false;
		}
	}
	
	/**
	 * getChildrenOfTag() gets the children of a passed tag
	 *
	 * @param tag_id is the tag to get the children of
	 *
	 * @return a xmls object containing the children tag
	*/
	function getChildrenOfTag($tag_id = "") {

		# Pages array to return
		$obTags = new Xmls();

		# Get pages without this parent
		$tags_array = $this -> getTagsWithValues(array("x_parent"), array($tag_id));

		# Add to the Pages object
		foreach ($tags_array as $key => $tag) {

			# Add
			$obTags -> addTag($tag);
		}

		# Return
		return $obTags;
	}
	
	/**
	 * getTagssWithValues() gets an array of tags that have the passed values for the passed keys
	 *
	 * @param key_array is an array of keys
	 * @param value_array is an array of values
	 *
	 * @return an array of Tags
	*/
	function getTagsWithValues($key_array = array(), $value_array = array()) {

		# Array to return
		$tag_array = array();

		# If any pages found...
		if (count($this -> xml_array) > 0) {

			# For each page
			foreach ($this -> xml_array as $tag_id => $tag) {

				# All values matching
				$values_matching = true;

				# For each key
				foreach ($key_array as $index => $key) {

					# If not matching...
					if ($tag -> getData($key) != $value_array[$index]) {

						$values_matching = false;
					}
				}

				# If all values matching
				if ($values_matching) {

					# Add page
					$tag_array[$tag_id] = $tag;
				}
			}
		}

		# Return
		return $tag_array;
	}
	
	/**
	 * getTagIDs() gets an array of tag IDs in the Xmls object
	 *
	 * @return an array of IDs
	*/
	function getTagIDs() {

		# For each page
		return array_keys($this -> xml_array);
	}
	
	/**
	*hasChildren() will return a bool to show whether current tag has children 
	*@param tag_id is id of current tag
	*$return ture/false
	*/
	function hasChildren($tag_id = ""){

		foreach($this -> xml_array as $tag_key => $tag){
			
			if($tag->getData('x_parent') == $tag_id){
				
				return true;
			}
		}
		
		return false;
	}
	
}

/**
 * Tag() contains data about a tag
*/
class Tag {

	# Store details in an array
	var $tag_id;
	var $tag_data = array();
	var $tag_atri = array();
	
	/**
	 * Tag() instantiates the Page object
	 *
	 * @param tag_id is the ID of the tag
	 * @param base_data is the base data to be added to the tag
	*/
	function Tag($tag_id = "", $base_data = array()) {

		# Check ID providedd
		if ($tag_id != "") {

			# Set ID
			$this -> tag_id = $tag_id;

			# For each bit of data
			foreach ($base_data as $data_key => $data_value) {

				# Add to array
				$this -> addData($data_key, $data_value);
			}
		}
	}
	
	/**
	 * AddAttribute() adds attribute into system
	 *
	 * @param att_name is the name for attribute
	 * @param att_value is the Value for the attribute
	*/
	function AddAttribute($att_name,$att_value){
		
		if ($att_name != "" && !isset($this -> tag_atri[$att_name])) {
		
			$this -> tag_atri[$att_name] = $att_value;
		}
	}
	
	function GetAttribute(){
		
		return $tag_atri;
	}
	
	/**
	 * addData() adds a piece of data
	 *
	 * @param data_key is the Key for the data
	 * @param data_value is the Value for the data
	*/
	function addData($data_key = "", $data_value = "") {

		# Ensure key passed and doesn't already exist
		if ($data_key != "" && !isset($this -> tag_data[$data_key])) {

			# Add
			$this -> tag_data[$data_key] = $data_value;
		}
	}
	
		
	/**
	 * getID() gets the ID of the page
	 *
	 * @return the ID of the page
	*/
	function getID() {

		# Return
		return $this -> tag_id;
	}
	
	/**
	 * getData() gets a piece of data
	 *
	 * @param data_key is the Key of the data to get
	 *
	 * @return the data, or false if not found
	*/
	function getData($data_key = "") {

		# Check if set
		if (isset($this -> tag_data[$data_key])) {

			# Return value
			return $this -> tag_data[$data_key];

		} else {

			# Return false
			return false;
		}
	}
}

/**
 * Attribute() contains data about an attribute
*/
class Attribute {
	
	var $att_name = "";
	var $att_value = "";

	function Attribute($name,$value){
		
		$att_name = $name;
		
		$att_value = $value;
	}
	
	function getAttributeName(){
		
		return $att_name;
	}
	
	function getAttributeValue(){
		
		return $att_value;
	}
}

/**
 * getAllXmlTags() gets the ID, name and parent of all the pages that are active, and places them in a Pages() object
*/
function getAllXmlTags($lag,$section) {
	
	# Globals
	global $PROJECT_DB_TABLES;

	# Create container
	$xmls = new Xmls();

	# SQL
	//$sql = "SELECT x_id,x_name,x_parent,x_access,x_status,x_section,x_sort,x_content,x_language,at_id,at_name,at_value FROM " . $PROJECT_DB_TABLES["tags"] . " tag LEFT JOIN ".$PROJECT_DB_TABLES["atri"]." atri ON tag.x_id=atri.at_xml_id WHERE x_status = 1 AND x_language = '" . $lag . "' ORDER BY x_sort,x_id ASC";
	$sql = "SELECT x_id,x_name,x_parent,x_access,x_status,x_section,x_sort,x_content,x_language FROM " . $PROJECT_DB_TABLES["tags"] . " WHERE x_status = 1 AND x_language = '" . $lag . "' AND x_section = '".$section."' ORDER BY x_sort,x_id ASC";

	# Get record
	$result = getRecord($sql, "x_id");
	
	$sql_attri = "SELECT at_xml_id,at_name,at_value,at_section,at_language FROM " . $PROJECT_DB_TABLES["atri"] . " WHERE at_language = '" . $lag . "' AND at_section = '".$section."'";
	
	$result_attri = getRecord($sql_attri);

	# Add pages
	foreach ($result as $key => $tag_array) {
		
		$newTag = new Tag($key, $tag_array);
		
		foreach($result_attri as $attri_array){
		
			if($attri_array['at_xml_id'] == $tag_array['x_id']){
		
				$newTag -> AddAttribute($attri_array['at_name'],$attri_array['at_value']);
			}
		}

		//AddAttribute
		
		$xmls -> addTag($newTag);
	}

	# Return
	return $xmls;
}


/**
* excXMLImport() Insert xml tags into database
*
* @param current xml path 
*/
function excXMLImport($fpath,$path){
	
	$doc = new DOMDocument;

	$doc->Load($fpath);

	$xpath = new DOMXPath($doc);

	$entries = $xpath->query($path);
}

/**
* getLangs() get languages of xml
* 
* @return the array  of the languages
*/
function getLangs(){
	
	# Globals
	global $PROJECT_DB_TABLES;
	
	# SQL
	$sql = "SELECT distinct x_language FROM " . $PROJECT_DB_TABLES["tags"] . "";
	
	# Get record
	$result = getRecord($sql);
	
	$array_Langs = array();
	
	foreach($result as $key => $value){
		
		if($value["x_language"] != "English"){

			array_push($array_Langs,$value["x_language"]);
		}
	}
	
	$array_Langs = array_reverse($array_Langs);
	
	array_push($array_Langs,"English");
	
	$array_Langs = array_reverse($array_Langs);
	
	return $array_Langs;
}

/**
* getXMLSections() get xml files which have been installed into database 
* 
* @return
*/
function getXMLSections(){
	
	# Globals
	global $PROJECT_DB_TABLES;
	
	# SQL
	$sql = "SELECT distinct x_section FROM " . $PROJECT_DB_TABLES["tags"] . "";
	
	# Get record
	$result = getRecord($sql);
	
	return $result;
}

?>